package com.xkdx.guangguang.fragment.comment;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.xkdx.caipiao.R;
import com.xkdx.guangguang.ManagerActivity;
import com.xkdx.guangguang.adapter.BrandMoreCommentAdapter;
import com.xkdx.guangguang.adapter.ShopCommentAdapter;
import com.xkdx.guangguang.fragment.BaseFragment;
import com.xkdx.guangguang.module.network.AbsModule;
import com.xkdx.guangguang.module.statics.IConstants;
import com.xkdx.guangguang.shareclass.BrandCommentary;
import com.xkdx.guangguang.shareclass.DaZhongDetailBean;
import com.xkdx.guangguang.shareclass.Shop;
import com.xkdx.guangguang.util.SharePrefenceUtil;
import com.xkdx.guangguang.wiget.XListView;
import com.xkdx.guangguang.wiget.XListView.IXListViewListener;

public class BrandMoreCommentFragment extends BaseFragment {
	View view;
	LayoutInflater inflater;
	int PageIndex = 1;
	String brandID, brandName, LoadMoreFlag;
	List<Shop> list = null;
	private Button btn_back;
	private Button btn_commit;
	List<BrandCommentary> brandCommentaryList;
	List<DaZhongDetailBean> daZhongList = new ArrayList<DaZhongDetailBean>();
	ShopCommentAdapter mShopCommAdapter;
	XListView shop_maincommentary;
	String num;
	SharePrefenceUtil su;
	int pageIndex = 1;
	private TextView tv_showCounts;
	public static boolean isLoadMore = false;
	ImageLoader imageLoader = ImageLoader.getInstance();
	DisplayImageOptions options;
	BrandMoreCommentAdapter brandCommentAdapter;
	BrandMoreCommentAction brandMoreCommentAction;
	BrandMoreCommentModule brandCommentModule;
	BrandMoreCommentPresistence brandMoreCommentPresistence;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		brandID = getArguments().getString("BrandID");
		brandName = getArguments().getString("BrandName");
		inflater = LayoutInflater.from(getActivity());

		options = new DisplayImageOptions.Builder()
				.showStubImage(R.drawable.shop_empty)
				.showImageForEmptyUri(R.drawable.shop_empty)
				.showImageOnFail(R.drawable.shop_empty)
				.resetViewBeforeLoading().cacheOnDisc()
				.imageScaleType(ImageScaleType.EXACTLY)
				.bitmapConfig(Bitmap.Config.RGB_565).build();

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		view = inflater.inflate(R.layout.shop_commmentarymoreinfos, container,
				false);
		findView();
		setOnClick();
		showLoading();
		return view;
	}

	@Override
	public void findView() {
		IConstants.hideInputMethodAways(getActivity(), view);
		btn_back = (Button) view.findViewById(R.id.btn_morecommentback);
		btn_commit = (Button) view.findViewById(R.id.btn_commentadd);
		tv_showCounts = (TextView) view.findViewById(R.id.tv_showcommentcounts);
		shop_maincommentary = (XListView) view
				.findViewById(R.id.strollshopcommentary_list);
		shop_maincommentary.setPullLoadEnable(true);
		brandCommentModule = new BrandMoreCommentModule();

	}

	@Override
	public void setOnClick() {
		btn_back.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				getFragmentManager().popBackStack();
			}
		});
		btn_commit.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				SharePrefenceUtil su = new SharePrefenceUtil(getActivity(),
						IConstants.SP_USER);
				String loginToken = su.getUserLoginToken();
				if (!loginToken.equals("")) {
					BrandCommentFragment brandCommentFragment = new BrandCommentFragment();
					Bundle b = new Bundle();
					b.putString("BrandID", brandID);
					b.putString("BrandName", brandName);
					brandCommentFragment.setArguments(b);
					getFragmentManager()
							.beginTransaction()
							.replace(R.id.fragment, brandCommentFragment,
									"brandCommentFragment")
							.addToBackStack(null).commit();

				} else {
					UserLogin();
				}

			}

		});

	}

	@Override
	public void showLoading() {
		dataLoadDialog.show();
		pageIndex = 1;

		brandMoreCommentAction = new BrandMoreCommentAction(brandID, IConstants.getCommentTime(),
				String.valueOf(pageIndex));

		brandMoreCommentPresistence = new BrandMoreCommentPresistence(this);
		((ManagerActivity) getActivity())
				.addCurrentTask(brandMoreCommentPresistence);
		brandMoreCommentPresistence.setActitons(brandMoreCommentAction);
		brandMoreCommentPresistence.setModule(brandCommentModule);
		brandMoreCommentPresistence.execute();
		dataLoadDialog.setOnCancelListener(new OnCancelListener() {

			@Override
			public void onCancel(DialogInterface dialog) {
				if (brandMoreCommentPresistence != null) {
					brandMoreCommentPresistence.cancel(true);
				}
				getFragmentManager().popBackStack();

			}
		});
		dataLoadDialog.show();
	}

	@Override
	public void showLoaded(HashMap<String, AbsModule> map) {
		dataLoadDialog.dismiss();
		if (map.containsKey("0")) {
			brandCommentaryList = brandCommentModule.brandList;
			if (brandCommentaryList.size() != 0) {
				brandCommentAdapter = new BrandMoreCommentAdapter(
						getActivity(), brandCommentaryList, imageLoader,
						brandID, brandName);
				shop_maincommentary.setAdapter(brandCommentAdapter);
				shop_maincommentary.setXListViewListener(xlistner);
				tv_showCounts.setText(brandCommentModule.brandCommentNum
						+ "条评论");
			}
		} else {
			refresh(map);
		}
		onLoad();
	}

	// 加载更多
	private void loadMore() {
		if (isLoadMore) {
			return;
		}
		pageIndex++;
		isLoadMore = true;
		brandMoreCommentAction = new BrandMoreCommentAction(brandID,
				IConstants.getCurrentTime(), String.valueOf(pageIndex));
		brandMoreCommentPresistence = new BrandMoreCommentPresistence(this);
		((ManagerActivity) getActivity())
				.addCurrentTask(brandMoreCommentPresistence);
		brandMoreCommentPresistence.setActitons(brandMoreCommentAction);
		brandMoreCommentPresistence.setModule(brandCommentModule);
		brandMoreCommentPresistence.execute();

	}

	@Override
	public void showClick(HashMap<String, AbsModule> map) {
	}

	@Override
	public void showLoadMore(HashMap<String, AbsModule> map) {
		isLoadMore = false;
		if (map.containsKey("0")) {
			if (brandCommentModule.brandList.size() != 0) {
				for (BrandCommentary brandCommentary : brandCommentModule.brandList) {
					brandCommentaryList.add(brandCommentary);
				}
				brandCommentAdapter.notifyDataSetChanged();
				tv_showCounts.setText(brandCommentModule.brandCommentNum
						+ "条评论");
			}
		} else {
			showErrorTip(map);
		}
		onLoad();
	}

	IXListViewListener xlistner = new IXListViewListener() {

		@Override
		public void onRefresh() {
			showLoading();

		}

		@Override
		public void onLoadMore() {
			loadMore();

		}
	};

	private void onLoad() {
		shop_maincommentary.stopRefresh(); // 下拉刷新
		shop_maincommentary.stopLoadMore();
	}

}
